const Compiler = require("./Compiler");
function webpack(config) {
  const argv = process.argv.slice(2);
  // 获取用户执行命令传递的参数
  const commandConfig = argv.reduce((acc, arg) => {
    let [key, value] = arg.split("=");
    key = key.replace(/^--/, "");
    acc[key] = value;
    return acc;
  }, {});
  // 根据webpack.config.js配置的参数进行合并
  const finalConfig = {
    ...config,
    ...commandConfig
  }
  // 初始化Compiler类
  const compiler = new Compiler(finalConfig);
  // 加载所有的插件, 插件进入webpack生命周期
  const plugins = finalConfig.plugins;
  for(let plugin of plugins) {
    plugin.apply(compiler);
  }
  return compiler
}

module.exports = webpack;
